<?php
namespace app\index\controller;
use app\common\lib\Redis;
use app\common\lib\redis\Predis;
use app\common\lib\Util;
use think\Exception;

class Login
{
    public function index()
    {
        //phone redis
        $phoneNum  = intval($_GET['phone_num']);
        $code = intval($_GET['code']);
        if(empty($phoneNum)|| empty($code))
        {
            return Util::show(config('code.error','phone or code is error'));
        }
        //redis code
        try {
            $redisCode = Predis::getInstance()->get(Redis::smsKey($phoneNum));
        }catch (\Exception $e)
        {
            return $e->getMessage();
        }
        /**
         * 校验验证码
         */
        if($redisCode == $code)
        {
            $data = [
                'user' =>$phoneNum,
                'srcKey'=>md5(Redis::userKey($phoneNum)),
                'time'  =>time(),
                'isLogin'=>true,
            ];
            //写入redis
            try {

                Predis::getInstance()->set(Redis::userKey($phoneNum),$data);
                return Util::show(config('code.success'),'Ok',$data);
            }catch (\Exception $e)
            {
                return  Util::show(config('code.error'),'error');
            }
        }else{
            return Util::show(config('code.error'),'login error');
        }


//       echo Predis::getInstance()->get(Redis::smsKey($phoneNum));

        //redis.so
    }
}